<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
	</body>
	 <script>
        /* 1、打印三角形图片 */
        function printTriangle() {
            for(var a = 0; a < 5; a++){
                var str = '';
                for(var b = 0; b < 4-a; b++){
                    str += ' ';
                }
                for(var b = 0; b < a*2-1; b++){
                    str += '*';
                }
                console.log(str);
            }
        }

        /* 2、使用switch和if else洗一个goHome()方法 */
        // switch
        function goHomeBySwitch(people) {
            var tool = '';
            switch(people) {
                case '老板':
                    tool = '坐小车回家';
                    break;
                case '白领':
                    tool = '坐高铁回家';
                    break;
                case '普工':
                    tool = '坐硬座回家';
                    break;
                default:
                    tool = '走路回家';
            }
            return tool;
        }

        // if else
        function goHomeByIfElse(people) {
            var tool = '';
            if(people == '老板') {
                tool = '坐小车回家';
            }else if(people == '白领') {
                tool = '坐高铁回家';
            }else if(people == '普工') {
                tool = '坐硬座回家';
            }else {
                tool = '走路回家';
            }
            return tool;
        }

        /* 3、使用三目运算符判断是否成年 */
        function isAdult(age) {
            var str = '';
            str = age >= 18 ? '成年人' : '未成年人';
            return str;
        }

        /* 4、加减乘除求模方法 */
        function init() {
            console.log(add(20, 10));
            console.log(sub(20, 10));
            console.log(mul(20, 10));
            console.log(div(20, 10));
            console.log(mod(16, 10));
        }
        function add(x, y) {
            return x + y;
        }
        function sub(x, y) {
            return x - y;
        }
        function mul(x, y) {
            return x * y;
        }
        function div(x, y) {
            return x / y;
        }
        function mod(x, y) {
            return x % y;
        }

        /* 7、实现瑞年算法 */
        function isLeapYear(year) {
            var str = '';
            if(typeof year == 'number') {
                if(year % 4 === 0 && year % 100 !== 0) {
                    str = year + '是闰年';
                }else {
                    str = year + '不是闰年';
                }
            }else {
                str = '传入的参数不是数字';
            }
            return str;
        }

        /* 9、排序(从小到大) 34 65 23 12 80 99 */
        // function sortArr() {
        //     var arr = [34, 65, 23, 12, 80, 99];
        //     arr.sort(function (a, b) {
        //         return a - b;
        //     })
        //     return arr;
        // }
        function sortArr() {
            var arr = [34, 65, 23, 12, 80, 99],
                len = arr.length,
                temp;
            for(var i = 0; i < len; i ++) {
                for(var j = i + 1; j < len; j ++) {
                    if(arr[i] > arr[j]) {
                        temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
            }
            return arr;
        }

        /* 10、实现随机查人算法(斗地主) */
        function douDiZhu() {
            var play = {
                player1: [],
                player2: [],
                player3: [],
                other: []
            }
            var pokeArr = createPoke();
            for(var i = 0; i < 17; i ++) {
                play.player1[i] = pokeArr.pop();
                play.player2[i] = pokeArr.pop();
                play.player3[i] = pokeArr.pop();
            }
            play.other = pokeArr;
            console.log('玩家1：' + play.player1 + '\n\n' + '玩家2：' + play.player2 + '\n\n' + '玩家3：' + play.player3 + '\n\n' + '底牌：' + play.other);
        }

        // 创建一副扑克并打乱
        function createPoke() {
            var colorArr = ['黑', '红', '梅', '方'],
                numArr = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'],
                pokeArr = ['小王', '大王'];

            // 自动生成一副有序扑克
            colorArr.forEach(function (colorEle, colorIndex) {
                numArr.forEach(function (numEle, numIndex) {
                    pokeArr.push(colorEle + numEle);
                })
            })

            // 打乱扑克顺序
            pokeArr.sort(function (a, b) {
                return Math.random() - 0.5;
            })
            return pokeArr;
        }
    </script>
</html>
